package com.hamster.tools.i18nManger.model
{
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.mediator.AppMediator;
	import com.hamster.tools.i18nManger.utils.AppUtil;
	import com.hamster.tools.i18nManger.vo.TranslateConfVO;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class TranslateConfVOProxy extends Proxy
	{
		public static const NAME:String = "TranslateConfVOProxy";
		
		public var translateConfVO:TranslateConfVO = new TranslateConfVO();
		
		public function TranslateConfVOProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function accessToken():void
		{
			var urlRequest:URLRequest = new URLRequest("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/");
			urlRequest.method = URLRequestMethod.POST;
			urlRequest.data = "client_id=" + encodeURIComponent(translateConfVO.client_id)
							+ "&client_secret=" + encodeURIComponent(translateConfVO.client_secret)
							+ "&scope=" + encodeURIComponent(translateConfVO.scope)
							+ "&grant_type=" + encodeURIComponent(translateConfVO.grant_type);
			var urlLoader:URLLoader = new URLLoader(urlRequest);
			urlLoader.addEventListener(Event.COMPLETE, accessTokenLoadCompleteHandler);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenLoadErrorHandler);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, accessTokenLoadErrorHandler);
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			urlLoader.load(urlRequest);
			AppUtil.showProgressBar("Loading access token from Microsoft Translate Service...", appMediator.app);
		}
		
		private function accessTokenLoadCompleteHandler(event:Event):void
		{
			AppUtil.hideProgressBar();
			var urlLoader:URLLoader = URLLoader(event.currentTarget);
			var resultObject:Object = JSON.parse(urlLoader.data);
			this.translateConfVO.access_token = resultObject.access_token;
			translateConfVO.expires_in = resultObject.expires_in;
			translateConfVO.request_time = new Date().time / 1000;
			
			this.sendNotification(AppEvent.TRANSLATE_ACCESS_TOKEN_RESPONSE);
		}
		
		private function accessTokenLoadErrorHandler(event:Event):void
		{
			AppUtil.hideProgressBar();
			this.sendNotification(AppEvent.TRANSLATE_ACCESS_TOKEN_FAULT);
		}
		
		public function translate(text:String, from:String, to:String):void
		{
			var urlRequest:URLRequest = new URLRequest("http://api.microsofttranslator.com/V2/Ajax.svc/Translate");
			urlRequest.method = URLRequestMethod.GET;
			if (to == "zh_CN") {
				to = "zh-CHS";
			} else if (to == "zh_TW") {
				to = "zh-CHT";
			}
			urlRequest.data =
				"appId=Bearer " + encodeURIComponent(translateConfVO.access_token) +
				"&from=" + encodeURIComponent(from) +
				"&to="   + encodeURIComponent(to)   +
				"&text=" + encodeURIComponent(text) +
				"&oncomplete=";
			var urlLoader:URLLoader = new URLLoader(urlRequest);
			urlLoader.addEventListener(Event.COMPLETE, translateCompleteHandler);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, translateErrorHandler);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, translateErrorHandler);
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			urlLoader.load(urlRequest);
			AppUtil.showProgressBar("Translating...", appMediator.app);
		}
		
		private function translateCompleteHandler(event:Event):void
		{
			AppUtil.hideProgressBar();
			var urlLoader:URLLoader = URLLoader(event.currentTarget);
			var resultObject:Object = JSON.parse(urlLoader.data);
			this.sendNotification(AppEvent.TRANSLATE_RESPONSE, resultObject);
		}
		
		private function translateErrorHandler(event:Event):void
		{
			AppUtil.hideProgressBar();
			trace (event);
			this.sendNotification(AppEvent.TRANSLATE_FAULT);
		}
		
		public function get appMediator():AppMediator
		{
			return facade.retrieveMediator(AppMediator.NAME) as AppMediator;
		}
		
	}
}